
package com.life.system.intercept;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.life.system.action.LoginAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {

	/* (non-Javadoc)
	 */
	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		// TODO Auto-generated method stub
		Object action = actionInvocation.getAction();
		ActionContext ctx = ActionContext.getContext();
		if(action instanceof LoginAction){
			return actionInvocation.invoke();
		}
		HttpServletRequest request =  (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		Object userSession  =  request.getSession().getAttribute("user");
		if(userSession != null){
			return actionInvocation.invoke();
		}else{
			return Action.LOGIN;
		}
	}

}
